<%@page import="javax.sql.rowset.CachedRowSet"%>
<%@page import="javax.sql.rowset.RowSetProvider"%>
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%
    //变量声明
    CachedRowSet rt = RowSetProvider.newFactory().createCachedRowSet(); //变量声明
    int intPageSize = 1; //一页显示的记录数
    int intRowCount = 0; //记录总数
    int intPageCount = 0; //总页数
    int intPage = 1; //待显示页码
    java.lang.String strOP;
    //取得待显示页码
    strOP = request.getParameter("op");
    if (strOP == null) {
        //表明在QueryString中没有page这一个参数
    intPage = 1;  
    session.setAttribute("currentpage", intPage);
    rt.setUsername("root");
    rt.setPassword("1234");
    rt.setUrl("jdbc:mysql://localhost:3306/bookstore");
    rt.setCommand("SELECT cust_id,cust_phone FROM bs_customer");
    //rt.setPageSize(intPageSize);
    rt.execute();
    //获取记录总数
    intRowCount = rt.size();
        //记算总页数
    intPageCount = (intRowCount + intPageSize - 1) / intPageSize;
    /////////////
        rt.setPageSize(intPageSize);
        rt.execute();
        session.setAttribute("data", rt);
        session.setAttribute("pagecount", intPageCount);}
    else{
        intPageCount = ((Integer)(session.getAttribute("pagecount"))).intValue();
        rt = (CachedRowSet) session.getAttribute("data");
        intPage = ((Integer) (session.getAttribute("currentpage"))).intValue();
        if(strOP.equals("pre"))
        {
            intPage = intPage - 1;
            session.setAttribute("currentpage", intPage);
            rt.previousPage();
        } else {
            intPage = intPage + 1;
            session.setAttribute("currentpage", intPage);
            rt.nextPage();
        }
    }
%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    </head>
    <body topmargin="0" leftmargin="0" >
        <table width="100%"><tr><td >
                    页次：<%=intPage%>/<%=intPageCount%>页  <%=intPageSize%>条/页  
                </td></tr></table>
        <table width="100%">
            <tr>
                <td width＝50%>ID</td>
                <td width＝50%>电话</td>
            </tr>
            <%
                while (rt.next()) {
                    String id = rt.getString(1);
                    String phone = rt.getString(2);
            %>
            <trwidth>
                <td width＝50%><%=id%></td>
                <td width＝50%><%=phone%></td>
                <tr>
                    <%
                        }
                    %>
                    </table>
                    <%if (intPage > 1) {
                    %>
                <a href="page2.jsp?op=pre">上一页</a>
                <%}%>
                <%if (intPage < intPageCount) {%>
                <a href="page2.jsp?op=last">下一页</a>
                <%}%>
                </body>
</html>